package com.huang.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.huang.yygh.hosp.repository.DepartmentRepository;
import com.huang.yygh.hosp.service.DepartmentService;
import com.huang.yygh.hosp.service.HospitalService;
import com.huang.yygh.model.hosp.Department;
import com.huang.yygh.model.hosp.Schedule;
import com.huang.yygh.vo.hosp.BookingScheduleRuleVo;
import com.huang.yygh.vo.hosp.DepartmentQueryVo;
import com.huang.yygh.vo.hosp.DepartmentVo;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @author HuangShen
 * @description: 科室业务层
 * @date 2021/5/4 11:04
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentRepository departmentRepository;



    @Override
    public void save(Map<String, Object> paramMap) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);
        Department targetDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if(null != targetDepartment) {
            //copy不为null的值，该方法为自定义方法
            BeanUtils.copyProperties(department, targetDepartment, Department.class);
            departmentRepository.save(targetDepartment);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> selectPage(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        //0为第一页
        Pageable pageable = PageRequest.of(page-1, limit, sort);

        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);


        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        //创建实例
        Example<Department> example = Example.of(department, matcher);
        Page<Department> pages = departmentRepository.findAll(example, pageable);
        return pages;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(null != department) {
            //departmentRepository.delete(department);
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {

        ArrayList<DepartmentVo> result = new ArrayList<>();
        Department probe = new Department();
        probe.setHoscode(hoscode);
        Example<Department> example = Example.of(probe);
        List<Department> departments = this.departmentRepository.findAll(example);
        Map<String, List<Department>> collects = departments.stream().collect(Collectors.groupingBy(Department::getBigcode));





        Set<Map.Entry<String, List<Department>>> entries = collects.entrySet();
        entries.forEach(stringListEntry -> {
            String key = stringListEntry.getKey();
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(key);
            departmentVo.setDepname(stringListEntry.getValue().get(0).getBigname());
            ArrayList<DepartmentVo> de = new ArrayList<>();
            stringListEntry.getValue().forEach(department -> {
                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepcode(department.getDepcode());
                departmentVo1.setDepname(department.getDepname());
                de.add(departmentVo1);


            });

            departmentVo.setChildren(de);

            result.add(departmentVo);


        });

        return result;
    }

    //根据科室编号，和医院编号，查询科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }







}
